<?php
/*
 * program:	均源
 * author:	雨中漫步 & 上善若水 & qhxin
 * email:	qhxin@qhxin.com
 * date:	2014/11/11
 * description:	又是一年双十一，不能剁手，就写点东西吧~~~~~~
 */

defined('IN_SCHOOL') or die();

/*{TranslateTagBegin}*/
class model_plugin extends base_model{
	//这是一个没有支持db的mod~~~

	/*
	$plg_conf = array(
		'baseinfo'	=>array(),
		'controls'	=>array(),
		'models'	=>array(),
		'template'	=>array(),
		'phphooks'	=>array(),
		'tplhooks'	=>array(),
	);
	*/
	
	public function plugin_install($plugin_name = '', $refresh = FALSE) {
		if($plugin_name == '') return FALSE;
		
		$plg_conf_file = CONFIG_PLG_PATH. $plugin_name.'/conf.php';
		if(!file_exists($plg_conf_file)) return FALSE;
		$plg_self_conf = include($plg_conf_file);
	
		$errno = 0;
		!isset($plg_self_conf['baseinfo']) && empty($plg_self_conf['baseinfo']) && $errno = 1;
		if($errno) return FALSE;
		
		$plg_conf_json_file = CONFIG_PATH. 'config_plugin.json';
		$plg_conf = file_exists($plg_conf_json_file) ? (array)core::json_decode(file_get_contents($plg_conf_json_file)) : array();
		$rand = max(0,abs($plg_self_conf['baseinfo']['rand']));
		$plg_conf['baseinfo'][$rand] = $plg_self_conf['baseinfo'];
		$plg_conf['controls'][$rand] = $plg_self_conf['controls'];
		$plg_conf['models'][$rand] = $plg_self_conf['models'];
		$plg_conf['template'][$rand] = $plg_self_conf['template'];
		$plg_conf['phphooks'][$rand] = $plg_self_conf['phphooks'];
		$plg_conf['tplhooks'][$rand] = $plg_self_conf['tplhooks'];
		
		$errno = 0;
		!ksort($plg_conf['baseinfo']) && $errno = 1;
		!ksort($plg_conf['controls']) && $errno = 1;
		!ksort($plg_conf['models']) && $errno = 1;
		!ksort($plg_conf['template']) && $errno = 1;
		!ksort($plg_conf['phphooks']) && $errno = 1;
		!ksort($plg_conf['tplhooks']) && $errno = 1;
		if($errno) return FALSE;
	
		$result = TRUE;
		if(!$refresh) {
			$install_file = CONFIG_PLG_PATH. $plugin_name.'/install.php';
			if(file_exists($install_file)){
				$result = include($install_file);
			}
		}
		if($result)
			file_put_contents($plg_conf_json_file,core::json_encode($plg_conf),LOCK_EX);
		return $result;
	}
	
	public function plugin_uninstall($plugin_name = '') {
	
		if($plugin_name == '') return FALSE;
		
		$plg_conf_file = CONFIG_PLG_PATH. $plugin_name.'/conf.php';
		if(!file_exists($plg_conf_file)) return FALSE;
		$plg_self_conf = include($plg_conf_file);
	
		$errno = 0;
		!isset($plg_self_conf['baseinfo']) && empty($plg_self_conf['baseinfo']) && $errno = 1;
		if($errno) return FALSE;
		
		$plg_conf_json_file = CONFIG_PATH. 'config_plugin.json';
		$plg_conf = file_exists($plg_conf_json_file) ? (array)core::json_decode(file_get_contents($plg_conf_json_file)) : array();
		$rand = max(0,abs($plg_self_conf['baseinfo']['rand']));
		unset($plg_conf['baseinfo'][$rand]);
		unset($plg_conf['controls'][$rand]);
		unset($plg_conf['models'][$rand]);
		unset($plg_conf['template'][$rand]);
		unset($plg_conf['phphooks'][$rand]);
		unset($plg_conf['tplhooks'][$rand]);
	
		$uninstall_file = CONFIG_PLG_PATH. $plugin_name.'/uninstall.php';
		$result = TRUE;
		if(file_exists($uninstall_file)){
			$result = include($uninstall_file);
		}
		if($result)
			file_put_contents($plg_conf_json_file,core::json_encode($plg_conf),LOCK_EX);
		
		return $result;
	
	}

}
/*{TranslateTagEnd}*/

?>